
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PUB;
using Apps.Models.PUB;
using Apps.IBLL.PUB;
namespace Apps.BLL.PUB
{
	public partial class PUB_EQUIPMENTBLL: Virtual_PUB_EQUIPMENTBLL,IPUB_EQUIPMENTBLL
	{
        

	}
    public class Virtual_PUB_EQUIPMENTBLL
    {
        [Dependency]
        public IPUB_EQUIPMENTRepository m_Rep { get; set; }

        public virtual List<PUB_EQUIPMENTModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<PUB_EQUIPMENT> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                                a => a.EQUIPMENT_NAME.Contains(queryStr)
                                || a.MODEL.Contains(queryStr)
                                || a.KKS_CODE.Contains(queryStr)
                                || a.PRODOCTION.Contains(queryStr)
                                || a.FCODE.Contains(queryStr)
                                || a.COMPANY.Contains(queryStr)
                                || a.PLACE.Contains(queryStr)
                                || a.FIXING_TYPE.Contains(queryStr)
                                );
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }


        public virtual List<PUB_EQUIPMENTModel> GetPagerList(ref GridPager pager, Expression<Func<PUB_EQUIPMENT, bool>> whereLambda)
        {
            IQueryable<PUB_EQUIPMENT> queryData = m_Rep.GetList();

            queryData = queryData.Where(whereLambda.Compile()).AsQueryable();

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public virtual List<PUB_EQUIPMENTModel> GetList(Expression<Func<PUB_EQUIPMENT, bool>> whereLambda)
        {

            IQueryable<PUB_EQUIPMENT> queryData = m_Rep.GetList(whereLambda);

            return CreateModelList(ref queryData);
        }

        public virtual List<PUB_EQUIPMENTModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            return new List<PUB_EQUIPMENTModel>();
        }

        public virtual List<PUB_EQUIPMENTModel> CreateModelList(ref IQueryable<PUB_EQUIPMENT> queryData)
        {

            List<PUB_EQUIPMENTModel> modelList = (from r in queryData
                                                  select new PUB_EQUIPMENTModel
                                                  {
                                                      EQUIPMENT_ID = r.EQUIPMENT_ID,
                                                      EQUIPMENT_NAME = r.EQUIPMENT_NAME,
                                                      MODELTYPE = r.MODEL,
                                                      KKS_CODE = r.KKS_CODE,
                                                      FDATE = r.FDATE,
                                                      PRODOCTION = r.PRODOCTION,
                                                      FCODE = r.FCODE,
                                                      COMPANY = r.COMPANY,
                                                      PLACE = r.PLACE,
                                                      FIXING_TYPE = r.FIXING_TYPE,
                                                      USED_DATE = r.USED_DATE,
                                                      CYCLE = r.CYCLE,
                                                      DEPARTMENT_ID = r.DEPARTMENT_ID,
                                                      USER_ID = r.USER_ID,
                                                      UPDATE_DATE = r.UPDATE_DATE,

                                                  }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, PUB_EQUIPMENTModel model)
        {
            try
            {

                PUB_EQUIPMENT entity = m_Rep.GetById(model.EQUIPMENT_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                var models = m_Rep.GetList(m => (m.KKS_CODE == model.KKS_CODE) || (m.EQUIPMENT_NAME == model.EQUIPMENT_NAME && m.FCODE == model.FCODE)).ToList();
                if (!string.IsNullOrEmpty(model.KKS_CODE) && models.Where(m => m.KKS_CODE == model.KKS_CODE).Count() > 0)
                {
                    errors.Add("KKS编码已存在");
                    return false;
                }

                if (models.Where(m => m.EQUIPMENT_NAME == model.EQUIPMENT_NAME && m.FCODE == model.FCODE).Count() > 0)
                {
                    errors.Add("已存在出厂编号和设备名称相同的设备");
                    return false;
                }
                entity = new PUB_EQUIPMENT();
                entity.EQUIPMENT_ID = model.EQUIPMENT_ID;
                entity.EQUIPMENT_NAME = model.EQUIPMENT_NAME;
                entity.MODEL = model.MODELTYPE;
                entity.KKS_CODE = model.KKS_CODE;
                entity.FDATE = model.FDATE;
                entity.PRODOCTION = model.PRODOCTION;
                entity.FCODE = model.FCODE;
                entity.COMPANY = model.COMPANY;
                entity.PLACE = model.PLACE;
                entity.FIXING_TYPE = model.FIXING_TYPE;
                entity.USED_DATE = model.USED_DATE;
                entity.CYCLE = model.CYCLE;
                entity.DEPARTMENT_ID = model.DEPARTMENT_ID;
                entity.USER_ID = model.USER_ID;
                entity.UPDATE_DATE = model.UPDATE_DATE;


                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Deletes(ref ValidationErrors errors, Expression<Func<PUB_EQUIPMENT, bool>> whereLambda)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {

                    foreach (var entity in db.PUB_EQUIPMENT.Where(whereLambda))
                    {
                        db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                    }

                    if (db.SaveChanges() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Edit(ref ValidationErrors errors, PUB_EQUIPMENTModel model)
        {
            try
            {
                PUB_EQUIPMENT entity = m_Rep.GetById(model.EQUIPMENT_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }

                var models = m_Rep.GetList(m => ((m.KKS_CODE == model.KKS_CODE) || (m.EQUIPMENT_NAME == model.EQUIPMENT_NAME && m.FCODE == model.FCODE)) && m.EQUIPMENT_ID != model.EQUIPMENT_ID).ToList();
                if (!string.IsNullOrEmpty(model.KKS_CODE) && models.Where(m => m.KKS_CODE == model.KKS_CODE).Count() > 0)
                {
                    errors.Add("KKS编码已存在");
                    return false;
                }

                if (models.Where(m => m.EQUIPMENT_NAME == model.EQUIPMENT_NAME && m.FCODE == model.FCODE).Count() > 0)
                {
                    errors.Add("已存在出厂编号和设备名称相同的设备");
                    return false;
                }

                entity.EQUIPMENT_ID = model.EQUIPMENT_ID;
                entity.EQUIPMENT_NAME = model.EQUIPMENT_NAME;
                entity.MODEL = model.MODELTYPE;
                entity.KKS_CODE = model.KKS_CODE;
                entity.FDATE = model.FDATE;
                entity.PRODOCTION = model.PRODOCTION;
                entity.FCODE = model.FCODE;
                entity.COMPANY = model.COMPANY;
                entity.PLACE = model.PLACE;
                entity.FIXING_TYPE = model.FIXING_TYPE;
                entity.USED_DATE = model.USED_DATE;
                entity.CYCLE = model.CYCLE;
                entity.DEPARTMENT_ID = model.DEPARTMENT_ID;
                entity.USER_ID = model.USER_ID;
                entity.UPDATE_DATE = model.UPDATE_DATE;



                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual PUB_EQUIPMENTModel GetById(object id)
        {
            if (IsExists(id))
            {
                PUB_EQUIPMENT entity = m_Rep.GetById(id);
                PUB_EQUIPMENTModel model = new PUB_EQUIPMENTModel();
                model.EQUIPMENT_ID = entity.EQUIPMENT_ID;
                model.EQUIPMENT_NAME = entity.EQUIPMENT_NAME;
                model.MODELTYPE = entity.MODEL;
                model.KKS_CODE = entity.KKS_CODE;
                model.FDATE = entity.FDATE == null ? DateTime.Now : Convert.ToDateTime(entity.FDATE);
                model.PRODOCTION = entity.PRODOCTION;
                model.FCODE = entity.FCODE;
                model.COMPANY = entity.COMPANY;
                model.PLACE = entity.PLACE;
                model.FIXING_TYPE = entity.FIXING_TYPE;
                model.USED_DATE = entity.USED_DATE == null ? DateTime.Now : Convert.ToDateTime(entity.USED_DATE);
                model.CYCLE = entity.CYCLE;
                model.DEPARTMENT_ID = entity.DEPARTMENT_ID;
                model.USER_ID = entity.USER_ID;
                model.UPDATE_DATE = entity.UPDATE_DATE == null ? DateTime.Now : Convert.ToDateTime(entity.UPDATE_DATE);
                model.DEV_CHECK = entity.DEV_CHECK;
                model.DEV_COME = entity.DEV_COME;
                model.DEV_EXPERIENCE = entity.DEV_EXPERIENCE;
                model.DEV_GREASE = entity.DEV_GREASE;
                model.DEV_MEMORABILIA = entity.DEV_MEMORABILIA;
                model.DEV_SPARE = entity.DEV_SPARE;
                model.DEV_TECHNICAL = entity.DEV_TECHNICAL;
                return model;
            }
            else
            {
                return null;
            }
        }

        public virtual PUB_EQUIPMENTModel GetItem(Expression<Func<PUB_EQUIPMENT, bool>> whereLambda)
        {

            PUB_EQUIPMENT entity = m_Rep.GetItem(whereLambda);
            if (entity == null) return null;

            PUB_EQUIPMENTModel model = new PUB_EQUIPMENTModel();
            model.EQUIPMENT_ID = entity.EQUIPMENT_ID;
            model.EQUIPMENT_NAME = entity.EQUIPMENT_NAME;
            model.MODELTYPE = entity.MODEL;
            model.KKS_CODE = entity.KKS_CODE;
            model.FDATE = entity.FDATE == null ? DateTime.Now : Convert.ToDateTime(entity.FDATE);
            model.PRODOCTION = entity.PRODOCTION;
            model.FCODE = entity.FCODE;
            model.COMPANY = entity.COMPANY;
            model.PLACE = entity.PLACE;
            model.FIXING_TYPE = entity.FIXING_TYPE;
            model.USED_DATE = entity.FDATE == null ? DateTime.Now : Convert.ToDateTime(entity.USED_DATE);
            model.CYCLE = entity.CYCLE == null ? 1 : Convert.ToInt16(entity.CYCLE);
            model.DEPARTMENT_ID = entity.DEPARTMENT_ID;
            model.USER_ID = entity.USER_ID;
            model.UPDATE_DATE = entity.FDATE == null ? DateTime.Now : Convert.ToDateTime(entity.UPDATE_DATE);

            return model;
        }


        /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<PUB_EQUIPMENTModel> list, ref ValidationErrors errors)
        {

            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.EQUIPMENT_ID, "EQUIPMENT_ID");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.EQUIPMENT_NAME, "EQUIPMENT_NAME");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.MODELTYPE, "MODEL");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.KKS_CODE, "KKS_CODE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.FDATE, "FDATE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.PRODOCTION, "PRODOCTION");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.FCODE, "FCODE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.COMPANY, "COMPANY");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.PLACE, "PLACE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.FIXING_TYPE, "FIXING_TYPE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.USED_DATE, "USED_DATE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.CYCLE, "CYCLE");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.DEPARTMENT_ID, "DEPARTMENT_ID");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.USER_ID, "USER_ID");
            excelFile.AddMapping<PUB_EQUIPMENTModel>(x => x.UPDATE_DATE, "UPDATE_DATE");

            //SheetName
            var excelContent = excelFile.Worksheet<PUB_EQUIPMENTModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new PUB_EQUIPMENTModel();
                entity.EQUIPMENT_ID = row.EQUIPMENT_ID;
                entity.EQUIPMENT_NAME = row.EQUIPMENT_NAME;
                entity.MODELTYPE = row.MODELTYPE;
                entity.KKS_CODE = row.KKS_CODE;
                entity.FDATE = row.FDATE;
                entity.PRODOCTION = row.PRODOCTION;
                entity.FCODE = row.FCODE;
                entity.COMPANY = row.COMPANY;
                entity.PLACE = row.PLACE;
                entity.FIXING_TYPE = row.FIXING_TYPE;
                entity.USED_DATE = row.USED_DATE;
                entity.CYCLE = row.CYCLE;
                entity.DEPARTMENT_ID = row.DEPARTMENT_ID;
                entity.USER_ID = row.USER_ID;
                entity.UPDATE_DATE = row.UPDATE_DATE;

                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<PUB_EQUIPMENTModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        PUB_EQUIPMENT entity = new PUB_EQUIPMENT();
                        entity.EQUIPMENT_ID = model.EQUIPMENT_ID;
                        entity.EQUIPMENT_NAME = model.EQUIPMENT_NAME;
                        entity.MODEL = model.MODELTYPE;
                        entity.KKS_CODE = model.KKS_CODE;
                        entity.FDATE = model.FDATE;
                        entity.PRODOCTION = model.PRODOCTION;
                        entity.FCODE = model.FCODE;
                        entity.COMPANY = model.COMPANY;
                        entity.PLACE = model.PLACE;
                        entity.FIXING_TYPE = model.FIXING_TYPE;
                        entity.USED_DATE = model.USED_DATE;
                        entity.CYCLE = model.CYCLE;
                        entity.DEPARTMENT_ID = model.DEPARTMENT_ID;
                        entity.USER_ID = model.USER_ID;
                        entity.UPDATE_DATE = model.UPDATE_DATE;

                        db.PUB_EQUIPMENT.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
        public void Dispose()
        {

        }

    }
}
